package com.xm9m.ibczy.dsp.utils;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

/**
 * Created by baichuan on 2016/11/17.
 */

public class NetWorkUtils {

    /**
     * get int ip
     *
     * @param context
     * @return
     */
    public static int getIp(Context context) {
        WifiManager wifiManager = getWifiManager(context);
        WifiInfo info = wifiManager.getConnectionInfo();
        int ipaddress = info.getIpAddress();

        return info.getIpAddress();
    }

    /**
     * get mac address
     *
     * @param context
     * @return
     */
    public static String getMac(Context context) {
        WifiManager manager = getWifiManager(context);
        return manager.getConnectionInfo().getMacAddress();
    }


    public static String getIpString(int ipaddress) {
        return (ipaddress & 0xFF) + "." +
                ((ipaddress >> 8) & 0xFF) + "." +
                ((ipaddress >> 16) & 0xFF) + "." +
                (ipaddress >> 24 & 0xFF);
    }


    private static WifiManager getWifiManager(Context context) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (!wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }
        return wifiManager;
    }

}
